package ui.panels.errors;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import ui.panels.commands.SetEnablecommand;

/**
 * @author vladimir && alon
 *
 */
public class ErrorPanel extends JPanel{

	private String 		message = null;
	private JLabel 		messageLabel;
	private JButton		button;
	private ErrorFrame	myFrame;
	Logger log = Logger.getLogger(this.getClass());
	
	public ErrorPanel(ErrorFrame myFrame ,String message)
	{
		this.myFrame = myFrame;
		this.message = message;
		
		initialize();
	}
	
	
	/**
	 * Initialize the panel
	 */
	private void initialize()
	{
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(704, 147));
	
			messageLabel = new JLabel(message , JLabel.CENTER);
			messageLabel.setFont(new Font(Font.SANS_SERIF, Font.LAYOUT_NO_LIMIT_CONTEXT , 18));
		
			button = new JButton("OK");
			button.setFont(new Font(Font.SERIF, Font.TYPE1_FONT, 16));
				
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(20, 25)
				.addComponent(messageLabel, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
				.addGap(31)
				.addComponent(button, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(36, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(83, 83)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(messageLabel, GroupLayout.PREFERRED_SIZE, 557, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(241)
				        .addComponent(button, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 241, Short.MAX_VALUE)))
				.addContainerGap(64, 64));
		} catch (Exception e) {
			log.debug(e.toString());
		}
		
		//set the OK Button
		setListeners();
	}
	
	
	/**
	 * Set the components listeners
	 */
	private void setListeners()
	{
		button.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				//set enable the prime GUI , use via command pattern
				SetEnablecommand enableCommand = new SetEnablecommand();
				enableCommand.setArg(true);
				enableCommand.execute();	
				
				//close this error frame
				myFrame.dispose();
			}
		});
	}
	
	
}
